<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/8/13
 * Time: 17:29
 */

namespace app\api\controller\v9;

use app\api\controller\v2\Auth;
use app\common\lib\SensorsData;
use app\common\model\Recode;
use app\common\model\RecodeNote as RecodeNoteModel;
use think\Cache;

/**
 * 星盘备注管理控制器
 * Class RecodeNote
 * @package app\api\controller\v9
 */
class RecodeNote extends Auth
{
    /**
     * 查询该档案下所有笔记注释
     */
    public function index()
    {
        $rid = input('rid/d');
        $page = input('page/d', 1);
        $uid = $this->user['id'];

        if (!$rid)
        {
            return json(['code' => 10414, 'msg' => 'Recode is not exist.']);
        }
        if (!$uid)
        {
            return json(['code' => 10415, 'msg' => 'User is not exist..']);
        }

        $recode = (new Recode())->getRecode(['id' => $rid]);
        if (!$recode || $recode['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '档案不存在');
        }

        $RecodeNoteModel = new RecodeNoteModel();
        $data = $RecodeNoteModel->getListAll(['rid' => $rid, 'uid' => $uid, 'page' => $page], 1);
        return show(config('code.success'), '获取成功', $data);
    }

    /**
     * 添加笔记注释
     * @throws \Exception
     */
    public function add()
    {
        $content = input('content/s', '', 'htmlspecialchars');
        $rid = input('rid/d', 0);
        $time = time();
        $event_time = input('event_time', 0);
        $event_time = strtr($event_time, '.', '-');
        $from = input('from/s', '', 'htmlspecialchars');
        $uid = $this->user['id'];
        $RecodeNoteModel = new RecodeNoteModel();
        $data = [
            'uid' => $uid,
            'content' => $content,
            'rid' => $rid,
            'from' => $from,
            'create_time' => $time,
            'last_time' => $time,
            'event_time' => $event_time ? strtotime($event_time) : 0
        ];
        if (!$uid)
        {
            return show(config('code.error'), 'User is not exist.');
        }
        if (!$rid)
        {
            return show(config('code.error'), 'Recode is not exist.');
        }
        if (trim($content) == '')
        {
            return show(config('code.error'), 'Content can not be empty.');
        }
        $recode = (new Recode())->getRecode(['id' => $rid]);
        if (!$recode || $recode['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '档案不存在');
        }
//        $data['id'] = $RecodeNoteModel->insertGetId($data);

        $data['id'] = (new \app\common\cachemodel\RecodeNote())->SaveData($this->user['id'], $rid, $data);

        Cache::rm(RecodeNoteModel::REDIS_KEY_IDS . $rid);
        if (!$data['id'])
        {
            return show(config('code.error'), '操作失败');
        }
        $date = date('Y.m.d H:i', $time);
        $data['create_time'] = $date;
        $data['last_time'] = $date;
        $data['event_time'] = $event_time ?: '';
        SensorsData::track($this->user['id'], true, 'RecordNote', ['chart_name' => $from, 'note_content' => $data['content']]);
        return show(config('code.success'), '获取成功', $data);
    }

    /**
     * 修改笔记注释
     * @throws \Exception
     */
    public function edit()
    {
        $id = (input('id/d', 0));
        $content = htmlspecialchars(input('content'));
        $rid = input('rid/d', 0);
        $time = time();
        $RecodeNoteModel = new RecodeNoteModel();
        $data = [
            'content' => $content,
            'last_time' => $time,
        ];
        if (trim($content) == '')
        {
            return show(config('code.error'), 'Content can not be empty.');
        }
        $uid = $this->user['id'];

        $recode = (new Recode())->getRecode(['id' => $rid]);
        if (!$recode || $recode['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '档案不存在');
        }

        $n_data = $RecodeNoteModel->where(['id' => $id, 'uid' => $uid])->find();
        if (empty($n_data))
        {
            return show(config('code.error'), 'Recode Note is not exist.');
        }
//        $RecodeNoteModel->where(['id' => $id, 'uid' => $uid])->update($data);

        (new \app\common\cachemodel\RecodeNote())->UpdateData($id, $data);

        Cache::rm(RecodeNoteModel::REDIS_KEY_DETAIL . $id);

        $data['last_time'] = date('Y.m.d H:i', $time);
        return show(config('code.success'), 'Success.', $data);
    }

    /**
     * 统计注释的数量
     * @param $rid int 该档案下注释的数量
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function counts($rid)
    {
        $rid = (int)($rid);
        $uid = $this->user['id'];

        $RecodeNoteModel = new RecodeNoteModel();
        $count = $RecodeNoteModel->getCountAll(['rid' => $rid, 'uid' => $uid]);
        return show(config('code.success'), 'Success.', $count);
    }
}
